//
//  Util.m
//  OAProject
//
//  Created by yhj on 2017/2/21.
//  Copyright © 2017年 cdnunion. All rights reserved.
//

#import "Util.h"
#import <CommonCrypto/CommonDigest.h>


@implementation Util

// 获取当前的时间
+(NSString*)getCurrentTimes
{
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];

    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

    // 现在时间,你可以输出来看下是什么格式
    NSDate *datenow=[NSDate date];

    //----------将nsdate按formatter格式转成nsstring
    NSString *currentTimeStr=[formatter stringFromDate:datenow];

    return currentTimeStr;
}

// 时间转时间戳
+(NSString *)getTimeStampByDate:(NSString *)timeStr
{

    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:00"];

    NSDate *firstTimeDate=[formatter dateFromString:timeStr];
    NSString *firstTimeSp=[NSString stringWithFormat:@"%ld",(long)[firstTimeDate timeIntervalSince1970]];
    //NSLog(@"TimeSp :%@", firstTimeSp);

    return firstTimeSp;
}



// 一般加密
+(NSString *)md5String:(NSString *)str
{
    const char *password=[str UTF8String];
    unsigned char mdc[16];
    CC_MD5(password,(CC_LONG)strlen(password),mdc);
    NSMutableString *md5String=[NSMutableString string];
    for (int i=0;i<16;i++)
    {
        [md5String appendFormat:@"%02x",mdc[i]];
    }
    return md5String;
}

+(NSString *)md5StringBest:(NSString *)str
{
    const char *password=[str UTF8String];
    unsigned char mdc[16];
    CC_MD5(password,(CC_LONG)strlen(password),mdc);
    NSMutableString *md5String=[NSMutableString string];
    for (int i=0;i<16;i++)
    {
        [md5String appendFormat:@"%02x",mdc[i]^mdc[0]];
    }
    return md5String;
}

+(BOOL)saveObjectToUserDefault:(id)object forKey:(NSString *)key
{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:object forKey:key];
    return [defaults synchronize];
}

+(id)getUserDefaultObjectForKey:(NSString *)key{
    id object=[[NSUserDefaults standardUserDefaults] objectForKey:key];
    return object==nil?@"":object;
}

//width
+(CGFloat)getWidthByCurrentSize:(NSString *)text withHeight:(CGFloat)height withWidthLimit:(CGFloat)widthLimit withFont:(CGFloat)fontSize{

    UIFont *font = [UIFont systemFontOfSize:fontSize];

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil];

    //widthLimt :  1000;
    //根据字体的大小样式换行
    CGSize size = [text boundingRectWithSize:CGSizeMake(widthLimit, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dict context:nil].size;

    return size.width;
}


//height:
+(CGFloat)getHeightByCurrentSize:(NSString *)text withWidth:(CGFloat)width widthHeightLimit:(CGFloat)heightLimit withFont:(CGFloat)fontSize{

    //heightLimt:  1000;
    CGSize size=[text boundingRectWithSize:CGSizeMake(width,heightLimit) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{
                                                                                                                                                                     NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}context:nil].size;
    return size.height;
}

+(CGFloat)GetHeightWithText:(NSString *)text width:(CGFloat)width  limitHeight:(CGFloat)limitHeight font:(CGFloat)font
{
    CGSize size=[text boundingRectWithSize:CGSizeMake(width,limitHeight) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{
                                                                                                                                                                     NSFontAttributeName:[UIFont systemFontOfSize:font]}context:nil].size;
    return size.height;
}


//+(CGFloat)GetHeightWithText:(NSString *)text width:(CGFloat)width  limitHeight:(CGFloat)limitHeight font:(CGFloat)font
//{
//    CGSize size=[text boundingRectWithSize:CGSizeMake(width,limitHeight) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{
//                                                                                                                                                                     NSFontAttributeName:[UIFont systemFontOfSize:font]}context:nil].size;
//    return size.height;
//}

extern UIView *setViewCoord_Y(UIView *view,CGFloat y)
{
    CGRect rect=view.frame;
    rect.origin.y=y;
    view.frame=rect;
    return view;
}

extern UIView *setViewHeight_Add(UIView *view,CGFloat height)
{
    CGRect rect=view.frame;
    rect.size.height+=height;
    view.frame=rect;
    return view;
}

// 判断请求的域名是哪个
+(NSString *)GetBaseUrl
{
    NSString *baseUrl;
    BOOL isOA=[self loginAndLogout];
    if (isOA==NO)
    {
        baseUrl=kBaseLoginURL;
    }
    else
    {
        baseUrl=kBaseUrl;
    }
    return baseUrl;
}

+(BOOL)loginAndLogout
{
    BOOL IsloginAndLogout=NO;
    if ([Util getUserDefaultObjectForKey:ISLogin]||[Util getUserDefaultObjectForKey:ISLogout])
    {
        IsloginAndLogout=YES;
    }
    return IsloginAndLogout;
}

+(NSString *)TaskStatusTitleByNumber:(NSString *)number
{
    // -1
    if ([number isEqualToString:@"-1"])
    {
       return @"编辑中";
    }
    else if ([number isEqualToString:@"1"])
    {
        return @"待接收";
    }
//    else if ([number isEqualToString:@"2"])
//    {
//        return @"任务等待接受中"
//    }
    else if ([number isEqualToString:@"3"])
    {
//        return @"未开始";
        return @"进行中";
    }
    else if ([number isEqualToString:@"4"])
    {
        return @"进行中";
    }
    else if ([number isEqualToString:@"5"])
    {
        return @"暂停";
    }
    else if ([number isEqualToString:@"6"])
    {
        return @"拒绝";
    }
    else if ([number isEqualToString:@"7"])
    {
        return @"终止";
    }
    else if ([number isEqualToString:@"8"])
    {
        return @"待验收";
    }
    else if ([number isEqualToString:@"9"])
    {
        return @"待评价";
    }
    else if ([number isEqualToString:@"10"])
    {
        return @"完成";
    }
    return nil;
}


/*
 *改变uiiamge 的大小
 *Quartz-2D,栈空间
 */
+(UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size
{
    //创建一个指定大小的"上下文空间"
    UIGraphicsBeginImageContext(size);
    //将入参的img绘制在"上下文空间"中,//核心
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    //从"上下文空间"中创建新的uiimage
    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
    //将"上下文空间"出堆栈
    UIGraphicsEndImageContext();
    //end
    return newImage;
}

+ (id)transformedValue:(id)value
{

    double convertedValue = [value doubleValue];
    int multiplyFactor = 0;

    NSArray *tokens = [NSArray arrayWithObjects:@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB", @"ZB", @"YB",nil];

    while (convertedValue > 1024) {
        convertedValue /= 1024;
        multiplyFactor++;
    }

    return [NSString stringWithFormat:@"%4.2f %@",convertedValue, [tokens objectAtIndex:multiplyFactor]];
}


@end
